package com.origon360.Services;

import java.util.List;
import java.util.logging.Logger;

import javax.ejb.Stateless;
import javax.persistence.EntityManager;
import javax.persistence.PersistenceContext;

import com.origon.entities.Category;
import com.origon.entities.Country;
import com.origon.entities.Currency;
import com.origon.entities.Language;
import com.origon.entities.Shop;
import com.origon360.Services.ShopService;

@Stateless
public class ShopService {
	
	private static Logger logger = Logger.getLogger(ShopService.class.getName());
	
	@PersistenceContext
	EntityManager em;
	
	public Language findLanguageById(String value){
		return em.find(Language.class, value);
	}
	
	public Language findLanguageByName(String value){
		
			Language language = (Language) em.createQuery("FROM Language where name =:languageName").setParameter("languageName", value).getSingleResult();
			
			logger.info("The Code of "+language);
			
			logger.info("The Code of "+language.getName()+ "is"+language.getCode());
			
			return language;
	}
	
	public List<Currency> fetchCurrencies(){
		return em.createQuery("FROM Currency", Currency.class).getResultList();
	}
	
	public List<Category> fetchCategories(){
		return em.createQuery("FROM Category", Category.class).getResultList();
	}
	
	public List<Country> fetchCountries(){
		return em.createQuery("FROM Country", Country.class).getResultList();
	}

	
	public void createShop(Shop shop){		
		em.persist(shop);
	}
	
	public List<Shop>fetchShops(){		
		return em.createQuery("FROM Shop", Shop.class).getResultList();
	}
}
